/**
 * Created by Hong on 2016/12/15.
 */

/** 通用 **/
var common = {
    form: {
        submit: function (form, title) {
            common.post($(form).attr("src"), $(form).serialize(), title, function () {
                layer.alert(title, {icon: 6}, function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.common.reload();
                    parent.layer.close(index);
                });
            });
        }
    },
    post: function (url, data, title, success) {
        $.post(url, data, function (data) {
            if (data.success) {
                success(data.value);
            } else {
                layer.alert(data.message, {icon: 5});
            }
        }, function (data) {
            layer.alert(data.message, {icon: 5});
        }, "JSON");
    },
    reload: function () {
        location.reload(location.href);
    }
}

var quartz = {
    operate: {
        edit: function (form) {
            common.form.submit(form, "修改Quartz任务成功！");
        }
    },
    pause: function (obj) {
        var index = layer.confirm("你确定要暂停该任务吗？", {btn: ["暂停", "取消"]}, function () {
            var triggerName = $(obj).attr("triggerName");
            var triggerGroup = $(obj).attr("triggerGroup");
            common.post("/pause.json", {triggerName: triggerName, triggerGroup: triggerGroup}, "暂停成功！", function () {
                layer.close(index);
                index = layer.alert("暂停成功！", {icon: 6, closeBtn: 0}, function () {
                    common.reload();
                    layer.close(index);
                });
            });
        });
    },
    restart: function (obj) {
        var index = layer.confirm("你确定要重启该任务吗？", {btn: ["重启", "取消"]}, function () {
            var triggerName = $(obj).attr("triggerName");
            var triggerGroup = $(obj).attr("triggerGroup");
            common.post("/restart.json", {triggerName: triggerName, triggerGroup: triggerGroup}, "重启成功！", function () {
                layer.close(index);
                index = layer.alert("重启成功！", {icon: 6, closeBtn: 0}, function () {
                    common.reload();
                    layer.close(index);
                });
            });
        });
    },
    remove: function (obj) {
        var index = layer.confirm("你确定要删除该任务吗？", {btn: ["删除", "取消"]}, function () {
            var triggerName = $(obj).attr("triggerName");
            var triggerGroup = $(obj).attr("triggerGroup");
            common.post("/remove.json", {triggerName: triggerName, triggerGroup: triggerGroup}, "删除成功！", function () {
                layer.close(index);
                index = layer.alert("删除成功！", {icon: 6, closeBtn: 0}, function () {
                    common.reload();
                    layer.close(index);
                });
            });
        });
    },
    edit: function (obj) {
        var triggerName = $(obj).attr("triggerName");
        var triggerGroup = $(obj).attr("triggerGroup");
        layer_show("修改Quartz任务", "/edit.html?triggerName=" + triggerName + "&triggerGroup=" + triggerGroup);
    }
}
